/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.employee.model;

import com.common.json.JsonModel;
import com.entities.EventLibrary;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.json.simple.JSONObject;

/**
 *
 * @author NguyenVan
 */
@Stateless
public class EventLibraryFacade extends AbstractFacade<EventLibrary> implements EventLibraryFacadeLocal {
    @PersistenceContext(unitName = "BluePumpkinEJBPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public EventLibraryFacade() {
        super(EventLibrary.class);
    }

   @Override
    public List<String> fillEventsDataTable() {
        List<String> jsonlist = new ArrayList<String>();
        Query showAll = em.createNamedStoredProcedureQuery("EventLibrary.fillEventsDataTable");
        List<Object[]> results = showAll.getResultList();
        for (Object[] row : results) {
            int eventId = (int) row[0];
            String eventName = (String) row[1];
            String description = (String) row[2];
            String eventType = (String) row[3];
            Date timeS = (Date) row[4];
            Date timeE = (Date) row[5];
            int registered = (int) row[6];
            EventLibrary el = new EventLibrary(eventId, eventName, description, eventType, timeS, timeE);
            JSONObject jsonObj = JsonModel.eventsDataTableMessageSender(el, registered);
            String json = jsonObj.toString();
            jsonlist.add(json);
        }
        return jsonlist;
    }
    
}
